package com.chukun.gateway.core.filter.impl;

import com.chukun.gateway.core.context.GatewayContext;
import com.chukun.gateway.core.filter.Filter;
import com.chukun.gateway.core.filter.annotation.FilterDefinition;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;

import static com.chukun.gateway.common.constants.GlobalFilterConst.*;

/**
 * @author chukun
 * @version 1.0.0
 * @description 灰度的过滤器
 * @createTime 2023/12/23 17:43
 */
@Slf4j
@FilterDefinition(id=GRAY_FILTER_ID, name = GRAY_FILTER_NAME, order = GRAY_FILTER_ORDER)
public class GrayFilter implements Filter {

    private static final String GRAY_HEADER = "gray";

    @Override
    public void doFilter(GatewayContext ctx) throws Exception {
        if (ctx == null || ctx.getRequest() == null) {
            return;
        }
        String gray = ctx.getRequest().getHeaders().get(GRAY_HEADER);
        if (StringUtils.isBlank(gray)) {
            return;
        }
        if ("true".equals(gray)) {
           ctx.setGray(true);
        }
    }
}
